package com.ruoyi.pingjiao.service.impl;

import java.util.List;

import com.ruoyi.pingjiao.domain.CourseRequirement;
import com.ruoyi.pingjiao.domain.GraduationRequirement;
import com.ruoyi.pingjiao.mapper.GraduationRequirementMapper;
import com.ruoyi.pingjiao.service.IGraduationRequirementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程毕业要求Service业务层处理
 * 
 * @author zhangpeng
 * @date 2025-02-07
 */
@Service
public class GraduationRequirementServiceImpl implements IGraduationRequirementService
{
    @Autowired
    private GraduationRequirementMapper graduationRequirementMapper;

    /**
     * 查询课程毕业要求
     * 
     * @param requirementId 课程毕业要求主键
     * @return 课程毕业要求
     */
    @Override
    public GraduationRequirement selectGraduationRequirementByRequirementId(Long requirementId)
    {
        return graduationRequirementMapper.selectGraduationRequirementByRequirementId(requirementId);
    }

    /**
     * 查询课程毕业要求列表
     * 
     * @param graduationRequirement 课程毕业要求
     * @return 课程毕业要求
     */
    @Override
    public List<GraduationRequirement> selectGraduationRequirementList(GraduationRequirement graduationRequirement)
    {
        return graduationRequirementMapper.selectGraduationRequirementList(graduationRequirement);
    }

    /**
     * 新增课程毕业要求
     * 
     * @param graduationRequirement 课程毕业要求
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGraduationRequirement(GraduationRequirement graduationRequirement)
    {
        int rows = graduationRequirementMapper.insertGraduationRequirement(graduationRequirement);
        insertCourseRequirement(graduationRequirement);
        return rows;
    }

    /**
     * 修改课程毕业要求
     * 
     * @param graduationRequirement 课程毕业要求
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGraduationRequirement(GraduationRequirement graduationRequirement)
    {
        graduationRequirementMapper.deleteCourseRequirementByRequirementId(graduationRequirement.getRequirementId());
        int rows = graduationRequirementMapper.updateGraduationRequirement(graduationRequirement);
        insertCourseRequirement(graduationRequirement);
        return rows;
    }

    /**
     * 批量删除课程毕业要求
     * 
     * @param requirementIds 需要删除的课程毕业要求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGraduationRequirementByRequirementIds(Long[] requirementIds)
    {
        graduationRequirementMapper.deleteCourseRequirementByRequirementIds(requirementIds);
        return graduationRequirementMapper.deleteGraduationRequirementByRequirementIds(requirementIds);
    }

    /**
     * 删除课程毕业要求信息
     * 
     * @param requirementId 课程毕业要求主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGraduationRequirementByRequirementId(Long requirementId)
    {
        graduationRequirementMapper.deleteCourseRequirementByRequirementId(requirementId);
        return graduationRequirementMapper.deleteGraduationRequirementByRequirementId(requirementId);
    }

    /**
     * 新增课程毕业要求信息
     * 
     * @param graduationRequirement 课程毕业要求对象
     */
    public void insertCourseRequirement(GraduationRequirement graduationRequirement)
    {
        List<CourseRequirement> courseRequirementList = graduationRequirement.getCourseRequirementList();
        Long requirementId = graduationRequirement.getRequirementId();
        if (StringUtils.isNotNull(courseRequirementList))
        {
            List<CourseRequirement> list = new ArrayList<CourseRequirement>();
            for (CourseRequirement courseRequirement : courseRequirementList)
            {
                courseRequirement.setRequirementId(requirementId);
                list.add(courseRequirement);
            }
            if (list.size() > 0)
            {
                graduationRequirementMapper.batchCourseRequirement(list);
            }
        }
    }
}
